package com.ThreadState;
//线程观测
// 线程5个状态
/**
 *  Thread.State 枚举型接口
 * NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED
 * 在jvm里执行->RUNNABLE
 */
public class ThreadObserve {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
            //线程睡眠5s
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(200);
                    System.out.println("线程执行--");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程结束===========");
        },"TEST");
        //todo ThreadState
        Thread.State state = thread.getState();//获取状态
        System.out.println("启动前---"+state);
        thread.start();
        state = thread.getState();
        System.out.println("启动后--- "+state);

        //线程运行时
        while (state!=Thread.State.TERMINATED){
            Thread.sleep(150);
            state = thread.getState();
            System.out.println("TEST运行状态："+state);
        }

//        testObserve testObserve = new testObserve();
//        Thread t1 = new Thread(testObserve,"t1");
//        Thread t2 = new Thread(testObserve,"t2");
//        t2.start();
//        t1.start();
//        Thread.State state1 = t1.getState();
//        Thread.State state2 = t2.getState();
//        while (state1!=Thread.State.TERMINATED || state2!=Thread.State.TERMINATED){
//            Thread.sleep(100);
//            state1 = t1.getState();
//            state2 = t2.getState();
//            System.out.println("t1-VIP运行状态："+state1+"|| t2-普通-状态"+state2);
//
//        }
    }
}
class testObserve implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            try{
                Thread.sleep(100);
                System.out.println("VIP客户进入"+i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

